package com.yidu.BusinessParament.service.impl;

import com.yidu.BusinessParament.entity.Fund;
import com.yidu.BusinessParament.mapper.FundMapper;
import com.yidu.BusinessParament.service.FundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *基金设置实现层
 * @Version 1.0
 * @Author zsc
 * @Date 2022/12/5 13:36
 */
@Service
public class FundServiceImpl implements FundService {
    @Autowired
    private FundMapper fundMapper;

    /**
     *
     * @param page 当前页
     * @param limit 每页显示数
     * @param fund 基金对象
     * @return
     */
    public List<Fund> SelectFund(int page , int limit, Fund fund) {
        Map map=new HashMap ();
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        map.put("strWhere",buildStrWhere(fund));
        return fundMapper.SelectFund (map);
    }



    /**
     * 生成条件字符串
     * @param fund 生成条件的参数
     * @return 条件字符串  and fundId like '%%'
     */
    public String buildStrWhere(Fund fund){
        StringBuffer strWhere=new StringBuffer("");
        if(fund.getFundId ()!=null && !fund.getFundId ().equals(""))
        {
            strWhere.append(" and fundId like '%"+fund.getFundId ()+"%'");
        }
        if (fund.getFundType ()!=null && !fund.getFundType ().equals("")){
            strWhere.append(" and fundType="+fund.getFundType () );
        }
        return  strWhere.toString();
    }
    /**
     * 查询基金表行数 +根据id和类型
     * @param fund 基金对象
     * @return
     */
    public int SelectFundCount(Fund fund) {
        Map map=new HashMap();
        map.put("strWhere",buildStrWhere(fund));
        int count =fundMapper.SelectFundCount (map);
        return count;
    }

    /**
     * 新增基金数据
     * @param fund 基金对象
     * @return
     */
    public int InsertFund(Fund fund) {
        return fundMapper.InsertFund (fund);
    }


    /**
     * 删除基金数据
     * @param fundId 基金Id
     * @return
     */
    public int DeleteFund(String fundId) {
        //将id转为数据
        String[] split = fundId.split(",");
        //创建存放id的集合
        ArrayList<Object> list = new ArrayList<Object>();
        for (String id : split) {
            //将id存入集合
            list.add(id);
        }
        return fundMapper.DeleteFund (list);
    }

    /**
     * 修改基金数据
     * @param fund 基金对象
     * @return
     */
    public int UpdateFund(Fund fund) {
        return fundMapper.UpdateFund (fund);
    }
}
